package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class ChangeGroupInfoContract {
    public ChangeGroupInfoContract() {
    }

    public ChangeGroupInfoContract(String operatorID, String groupID, String name, String announce) {
        this.OperatorID=operatorID;
        this.GroupID=groupID;
        this.GroupName=name;
        this.Announce=announce;
    }

    public String OperatorID ;

    public String GroupID ;

    public String GroupName ;

    public String Announce ;

    /**
     * 消息转byte
     *
     * @return
     */
    public byte[] serialize() throws Exception {
        byte[] bAnnounce = Announce.getBytes(StandardCharsets.UTF_8);
        byte[] bGroupID = GroupID.getBytes(StandardCharsets.UTF_8);
        byte[] bGroupName = GroupName.getBytes(StandardCharsets.UTF_8);
        byte[] bOperatorID = OperatorID.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bAnnounce.length + 4 + bGroupID.length + 4 + bGroupName.length + 4+bOperatorID.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bAnnounce.length);
        body.writeBytes(bAnnounce);
        body.writeInt(bGroupID.length);
        body.writeBytes(bGroupID);
        body.writeInt(bGroupName.length);
        body.writeBytes(bGroupName);
        body.writeInt(bOperatorID.length);
        body.writeBytes(bOperatorID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }

    public void deSerialize(byte[] info) throws Exception {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.Announce= SerializeUtils.readStrIntLen(buffer);
        this.GroupID=SerializeUtils.readStrIntLen(buffer);
        this.GroupName=SerializeUtils.readStrIntLen(buffer);
        this.OperatorID=SerializeUtils.readStrIntLen(buffer);
    }
}

